<?php
namespace app\admin\controller;
use app\parent\admin;
use think\Db;

define('TABLE' , 'slide');

class Slide extends Admin{
	public function index(){
		if(!$this->isAjax()){
			$yuming = get_system('agency_system_yun_t_yuming');
            return view('index' , ['yuming' => $yuming]);
        }
        //模糊条件查询
        $where = $this->whereSql([
            'like' => ['path'],
            'time' => ['time'],
            'eq' => ['slide_show'],
        ]);

        $pa = $this->input(['page','limit'],1);
        $page = ($pa['page'] - 1) * $pa['limit'];
        $data = Db::name(TABLE)
            ->where($where)
            ->order('time desc')
            ->limit($page,$pa['limit'])
            ->select();
        //分页计算条数
        $count = Db::name(TABLE)->where($where)->count('id');
        return $this->ajaxReturn(0,'',$data,$count);
	}

    public function indexAdd(){
        if(!$this->isPost()){
            return view();
        }else{
            $yuming = get_system('agency_system_yun_t_yuming');
            $data = $this->unInput('id');
            if(isset($data['img'])){
                $data['img'] = $yuming . $data['img'];
            }
            $data['time'] = time();
            $ex = db(TABLE)->where(['sort'=>$data['sort']])->find();
            if($ex){
                return $this->ajaxReturn(1 , '排序重复！');
            }
            if($data['is_back']==1){
                db(TABLE)->where(['is_back'=>1])->update(['is_back'=>0]);
            }
            $res  = Db::name(TABLE)->insert($data);
            if($res){
                return $this->ajaxReturn(0 , '添加成功');
            }else{
                return $this->ajaxReturn(1 , '添加失败');
            }
        }
    }

    public function indexDel(){
        $id = $this->input('id',0); // 获取id

        $is = Db::name(TABLE)->where('id','in',$id)->delete();

        if($is){
            return $this->ajaxReturn(0,'删除成功');
        }else{
            return $this->ajaxReturn(1,'删除失败');
        }
    }

    public function indexUp(){
        if(!$this->isPost()){
            $id     = $this->input('id' , 0);
            $yuming = get_system('agency_system_yun_t_yuming');

            $data = Db::name(TABLE)->field('path , img , type, sort,slide_show,content,is_back')->where('id' , $id)->find();
            return view('index_up' , ['data' => $data , 'yuming' => $yuming]);
        }else{
            $id   = $this->input('id' , 0);
            $yuming = get_system('agency_system_yun_t_yuming');
            $data = $this->unInput('id');
            $map['sort'] = $data['sort'];
            $ex = db(TABLE)->where($map)->where('id','neq',$id)->find();
            if($ex){
                return $this->ajaxReturn(1 , '排序重复！');
            }
            if($data['is_back']==1){
                db(TABLE)->where(['is_back'=>1])->update(['is_back'=>0]);
            }
            if(isset($data['img'])){
                $data['img'] = $yuming . $data['img'];
            }
            $res = Db::name(TABLE)->where('id' , $id)->update($data);
            
            if($res){
                return $this->ajaxReturn(0 , '修改成功');
            }else{
                return $this->ajaxReturn(1 , '修改失败');
            }
        }
    }

    // public function banner(){
    //     if(!$this->isAjax()){
	// 		$yuming = get_system('agency_system_yun_t_yuming');
    //         return view('banner' , ['yuming' => $yuming]);
    //     }
    //     //模糊条件查询
    //     $where = $this->whereSql([
    //         'like' => ['path'],
    //         'time' => ['time'],
    //         'eq' => ['slide_show'],
    //     ]);

    //     $pa = $this->input(['page','limit'],1);
    //     $page = ($pa['page'] - 1) * $pa['limit'];
    //     $data = Db::name('house_banner')
    //         ->where($where)
    //         ->order('time desc')
    //         ->limit($page,$pa['limit'])
    //         ->select();
    //     //分页计算条数
    //     $count = Db::name('house_banner')->where($where)->count('id');
    //     return $this->ajaxReturn(0,'',$data,$count);
    // }

    // public function bannerUp(){
    //     $id = $this->input('id',0);
    //     if(!$this->isPost()){
    //         $data = Db::name('house_banner')->where('id',$id)->find();
    //         return view('bannerUp',['data'=>$data]);
    //     }
    //     $data = $this->unInput('id');
    //     $yuming = get_system('agency_system_yun_t_yuming');
    //     if(isset($data['img_url'])){
    //         $data['img_url'] = $yuming . $data['img_url'];
    //     }
    //     $is = Db::name('house_banner')->where('id',$id)->update($data);
    //     if($is){
    //         return $this->ajaxReturn(0,'操作成功');
    //     }else{
    //         return $this->ajaxReturn(1,'操作失败');
    //     }
    // }
}